package com.example.himalaya.presenters;

import static com.example.himalaya.base.BaseApplication.getHandler;

import com.example.himalaya.data.HistoryDao;
import com.example.himalaya.data.IHistoryDaoCallback;
import com.example.himalaya.interfaces.IHistoryPresenter;
import com.example.himalaya.interfaces.IHistoryPresenterCallback;
import com.example.himalaya.utils.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HistoryPresenter implements IHistoryPresenter, IHistoryDaoCallback {

    private static final String TAG = "HistoryPresenter";

    private List<IHistoryPresenterCallback> mCallbacks = new ArrayList<>();
    private final HistoryDao mHistoryDao;
    private List<Track> mCurrentHistories=null;
    private Track mCurrentAddTrack=null;

    private HistoryPresenter() {
        mHistoryDao = HistoryDao.getHistoryDao();
        mHistoryDao.setCallback(this);
        listHistories();
    }

    private static HistoryPresenter sHistoryPresenter;

    public static HistoryPresenter getHistoryPresenter() {
        if (sHistoryPresenter == null) {
            synchronized (HistoryPresenter.class) {
                if (sHistoryPresenter == null) {
                    sHistoryPresenter = new HistoryPresenter();
                }
            }
        }
        return sHistoryPresenter;
    }


    @Override
    public void registerViewCallBack(IHistoryPresenterCallback iHistoryPresenterCallback) {
        if(!mCallbacks.contains(iHistoryPresenterCallback)) {
            mCallbacks.add(iHistoryPresenterCallback);
        }
    }

    @Override
    public void unRegisterViewCallBack(IHistoryPresenterCallback iHistoryPresenterCallback) {
        mCallbacks.remove(iHistoryPresenterCallback);
    }

    @Override
    public void listHistories() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.listHistories();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅

    }

    private void doAddHistory(final Track track) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.addHistory(track);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    private boolean isDoDelAsOutOfSize = false;//Do Del是否超出尺寸
    @Override
    public void addHistory(Track track) {
        //判断是否>=100条记录
        if (mCurrentHistories != null&&mCurrentHistories.size()>= Constants.MAX_HISTORY_COUNT) {
            //不能添加，先删除最前的一条记录，再添加
            this.mCurrentAddTrack = track;
            isDoDelAsOutOfSize = true;
            delHistory(mCurrentHistories.get(mCurrentHistories.size()-1));
        }else {
            doAddHistory(track);
        }
    }

    @Override
    public void delHistory(Track track) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.delHistory(track);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void cleanHistories() {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mHistoryDao != null) {
                    mHistoryDao.clearHistory();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }




    @Override
    public void onHistoryAdd(boolean isSuccess) {
        listHistories();
    }

    @Override
    public void onHistoryDel(boolean isSuccess) {
        if (isDoDelAsOutOfSize && mCurrentAddTrack!=null) {
            isDoDelAsOutOfSize = false;
            //添加当前的数据进到数据库里
            addHistory(mCurrentAddTrack);
        }
        listHistories();
    }

    @Override
    public void onHistoriesLoaded(List<Track> tracks) {
        mCurrentHistories = tracks;
        LogUtil.d(TAG,"历史尺寸 ==> "+tracks.size());
        //通知UI更新数据
        getHandler().post(new Runnable() {
            @Override
            public void run() {
                if (mCallbacks != null) {
                    for (IHistoryPresenterCallback callback : mCallbacks) {
                        callback.onHistoriesLoaded(tracks);
                    }
                }
            }
        });
    }

    @Override
    public void onHistoriesClean(boolean isSuccess) {
        listHistories();
    }
}
